<template>
  <div class="add-container">
    <el-form ref="form" :model="formData" :rules="rules" label-width="80px">
      <el-form-item prop="article_title" label="标题">
        <el-input v-model="formData.article_title" placeholder="请输入标题"></el-input>
      </el-form-item>
      <el-form-item prop="article_content" label="内容">
        <el-input v-model="formData.article_content" :rows="5" type="textarea"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submit">发表</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { addArticle } from '@/api/article'
import { mapGetters } from 'vuex'
export default {
  name: 'Add',
  data() {
    return {
      formData: {
        article_title: '',
        article_content: ''
      },
      rules: {}
    }
  },
  computed: {
    ...mapGetters(['userId']),
    ...mapGetters(['name'])
  },
  methods: {
    submit() {
      addArticle({ ...this.formData, userId: this.userId, name: this.name }).then((res) => {
        if (res.errcode === 0) {
          this.$router.push('/')
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.add-container {
  padding: 20px 10px;
}
</style>
